<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@include file="/WEB-INF/base.jspf"%>
<!DOCTYPE html>
<html>
<head>
<title>绑定域名</title>
<style>
	.modal-dialog {
		max-width: 1000px;
	}
.div-search-tool {
	display:flex;
	align-items:center;
}
.div-search-tool .form-control {
	width:200px;
	margin-right:6px;
}
#table-yumingList {
	margin-top:20px;
}
.control-btns button + button {
	margin-left:10px;
}
#addAndEditModal .p-c-flexwrapper {
	display:flex;
	align-items:center;
	margin-bottom:16px;
	margin-right:16px;
	width:100%;
}
#addAndEditModal .p-c-flexwrapper span {
	width:190px;
	text-align: center;
}
#addAndEditModal .p-c-flexwrapper .form-control {
}
#addAndEditModal .modal-body {
	display:flex;
	flex-wrap:wrap;
}
table th,table tr,table td {
	text-align: center;
	vertical-align:middle;
}
</style>
</head>
<body>
	<div class="div-search-tool">
		<input class="form-control" placeholder="域名" id="input-i-domain" />
		<input class="form-control" placeholder="所属代理" id="input-i-agency" />
		<button class="layui-btn layui-btn-radius" id="button-i-searchAJAXTable">查询</button>
		<button class="layui-btn layui-btn-radius" id="button-i-resetAJAXTable">重置</button>
	</div>
	<div style="margin-top:10px;">
		<button class="btn btn-primary" id="button-i-addNewDomain">新增</button>
	</div>
<table id="table-yumingList" class="class-tableDataTable table-normal-datatables table table-striped table-bordered table-hover">
		<thead>
			<tr>
				<th style="display:none;"></th>
				<th>域名</th>
				<th>默认代理</th>
				<th>域名状态</th>
<!-- 				<th>前台默认主页</th>
 -->				<th>创建时间</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
	
<!-- 提醒模态框（Modal） -->

<div class="modal fade" id="noticeModal" tabindex="-1" role="dialog" aria-labelledby="noticeModal" aria-hidden="true">
    <div class="modal-dialog class-fake-alert">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body" ></div>
            <div class="modal-footer">	      
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>	
<!-- 模态框（Modal） -->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            </div>
            <div class="modal-body">你确定要删除该选项吗？</div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" id="btn-i-delModalBtn">删除</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>
<!-- /.modal -->	

<!-- 模态框（Modal） -->
<div class="modal fade" id="addAndEditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            </div>
            <form>
            <div class="modal-body">
            	<input id="input-i-id" hidden="hidden" class="input-auto" />
            	<p class="p-c-flexwrapper"><span>域名：</span><input required id="input-modal-i-domain" class="input-auto form-control" /></p>
            	<p class="p-c-flexwrapper"><span>默认代理：</span><select required id="input-modal-i-defaultDaiLi" class="form-control"></select></p>
            	<!-- <p class="p-c-flexwrapper"><span>前台默认主页：</span><input class="form-control" /></p> -->
            </div>
            </form>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="button-i-addNewDomain-submit">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>
<!-- /.modal -->
<!--修改  -->
<div class="modal fade" id="addAndEditModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            </div>
            <form>
            <div class="modal-body">
            	<input id="input-i-id1" hidden="hidden" class="input-auto" />
            	<p class="p-c-flexwrapper"><span>代理域名：</span><input required id="input-modal-i-domain1" class="input-auto form-control" style="display: inline-block;width:auto;" /></p>
            </div>
            </form>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="button-i-addNewDomain-submit1">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>


<script type="text/javascript" src="/js/zhengzeTools.js"></script>
	<script>
	bindJinYongQiYongSwitch("#table-yumingList", "/admin/editDomainStatus.do", "status")
	bindDelItemEvent("#table-yumingList", ".btn-deleteDomainItem","/admin/deleteDomain.do")
		var dataTable = $("#table-yumingList").DataTable({
		      // "bPaginate": true,
		      "bLengthChange": true,      
		      "bSort": false,
		      "bAutoWidth": false,
		      'aLengthMenu':[5, 20],
		       "searching": false,
		       "dom": "t<'tableinfo'i<'tableSearch'p>l>",
		       "language": {//语言设置  
		            "lengthMenu": "每页显示 _MENU_ 条记录",     
		            "zeroRecords": "没有检索到数据",     
		            "sInfo": "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",     
		            "info": "没有数据",     
		            "oPaginate": {     
		                "sFirst": "首页",     
		                "sPrevious": "前一页",     
		                "sNext": "后一页",     
		                "sLast": "尾页"    
		          	},
		            "search":"搜索:",
		            "infoFiltered":   "(从  _MAX_ 记录中搜索出结果)",
		            "infoEmpty":      "没有找到匹配的信息",
		         },
		         "drawCallback": function( settings ) {
		        	 $(".input-c-mySwitch").bootstrapSwitch()
		         },
		         "ajax": {
		        	 "type": "POST",
		        	 "url": "/admin/findParkedDomainsList.do",
		        	 "dataSrc": function(json) {
		        		 var newJson = json.data
		        		 for (var i = 0; i < newJson.length; i++) {
		         			 var item = newJson[i]
		         			 var stateValue = ""
		        			 if (item.domainStatus == "1") {
		        				 stateValue = "checked"
		        			 } else {
		        				 stateValue = ""
		        			 }
		        		 	item.domainStatus = "<input class='input-c-mySwitch' data-size='small' type='checkbox' " + stateValue + " data-on-text='启用' data-on-color='success' data-off-color='danger' data-off-text='禁用' />"
		        		 }
		        		 return newJson
		        	 },
		         },
 		         "columns": [
							 {"data": "id", "className": "hidden-td id-edit-td"},
		                     {"data": "domain", "className": "domain-edit-td"},
		                     {"data": "defaultAgency", "className": "defaultAgency-edit-td"},
		                     {"data": "domainStatus",},
		                     {"data": "createdTime",},
		                     {"data": null, "defaultContent": "<button class='btn btn-warning btn-c-editZongDaiLiInfo btn-sm'>修改</button><button class='btn btn-danger btn-deleteDomainItem btn-sm'>删除</button>"},
		                 ]      
		   		 })	
		   		 insertSearchBtn()
		$("#button-i-addNewDomain").on("click", function(event) {
			var newRequest = {
				url: "/admin/findByStateGeneralAgentList.do",
				type: "post",
				data: {
				},
				success: function(data) {
					var newData = JSON.parse(data)
					if (newData.data.length == 0) {
						showNoticeModal(newData.message)
						return
					}
					$("#input-modal-i-defaultDaiLi").html("")
					for (var j = 0; j < newData.data.length; j++) {
						var item = newData.data[j]
						var html = "<option value='" + item + "'>" + item + "</option>"
						$("#input-modal-i-defaultDaiLi").append(html)
					}					
 					$("#addAndEditModal").attr("data-type", "add")
					$("#addAndEditModal").modal("show")
				},
				error: function(status) {
					
				}
			}
			ajax(newRequest)	
		})
		$('#addAndEditModal').on('hidden.bs.modal', function () {
			if ($("#addAndEditModal").attr("data-type") == "edit") {
				$("#addAndEditModal form")[0].reset()
			}
		})	
		
		var dailiYm;
		var id
		$("#table-yumingList").on("click",".btn-c-editZongDaiLiInfo",function(e){
			$("#addAndEditModal1").modal("show")	
			dailiYm=$("#input-modal-i-domain1").val($(this).parent().siblings(".domain-edit-td").text())
			id=$(this).parent().siblings(".id-edit-td").text()
		})
		
			$("#addAndEditModal1").on("click","#button-i-addNewDomain-submit1",function(e){
				$("#addAndEditModal1").modal("hide");
				$.ajax({
					type:"post",
					url:"/admin/editDomain.do",
					data:{
						"domain":$("#input-modal-i-domain1").val(),
						"id":id,
						},
					success:function(data){
						$("#table-yumingList").DataTable().ajax.url("/admin/findParkedDomainsList.do").load(); 
					    alert(data.message); 
					},
				})
			})
		
		var obj = {
			btnName: "#button-i-addNewDomain-submit",
			divName: "#addAndEditModal",
			isRegArray: [
	             {inputValue: function() {
	            	 return $("#input-modal-i-domain").val()
	             }, regValue:/[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/, noticeText:"无效的域名"},
	             ],
			}
		
		checkFormMethod(obj, function() {
			var newRequest = {
				type: "post",
				data: {
					domain: $("#input-modal-i-domain").val(),
					defaultAgency: $("#input-modal-i-defaultDaiLi").val(),
				},
				success: function(data) {
					var newData = JSON.parse(data)
					showNoticeModal(newData.message)
					if (newData.data == "1") {
						$('#addAndEditModal').modal("hide")
						$("#addAndEditModal form")[0].reset()
						var table = $("#table-yumingList").DataTable()
						table.ajax.reload()
					}
				},
				error: function(status) {
				}
			}
			newRequest.url = "/admin/addDomain.do"
			ajax(newRequest)
		})			
		bindSearchBtnAndResetBtnEvent({
			search: "#button-i-searchAJAXTable",
			reset: "#button-i-resetAJAXTable",
			table: "#table-yumingList",
			params: ["#input-i-domain", "#input-i-agency"],
		})
	</script>
</body>
</html>